import time

from common.BasePages import BasePage


class DevicePage(BasePage):
    # 设备管理
    def device_url(self):
        self.goto_url("projectManagement/deviceManagement")

    def create_device(self, gateway_name):
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("dialog", name="新增设备").get_by_placeholder("请输入项目名称").click()
        self.page.get_by_role("listitem").filter(has_text="张院龙").click()
        self.page.get_by_role("dialog", name="新增设备").get_by_placeholder("请输入设备名称").click()
        self.page.get_by_role("dialog", name="新增设备").get_by_placeholder("请输入设备名称").fill("张院龙设备")
        self.page.get_by_role("textbox", name="请选择网关").click()
        self.page.get_by_role("listitem").filter(has_text=f"{gateway_name}").click()
        self.page.get_by_role("textbox", name="请选择协议").click()
        self.page.get_by_role("listitem").filter(has_text="Modbus RTU").click()
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("新增成功")

    def edit_device(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_9_column_129 > .cell > .basicTableBtnBox > .el-icon-edit-outline").first.click()
        self.page.get_by_role("textbox", name="请输入设备别名").click()
        self.page.get_by_role("textbox", name="请输入设备别名").fill("设备别名")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("编辑成功")

    def delete_device(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_9_column_129 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确认").click()
        self.assert_alert_content("删除设备成功")
